package com.helloae.chap09;

import java.awt.BorderLayout;
import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;

import javax.swing.JFrame;

import com.esri.arcgis.beans.map.MapBean;
import com.esri.arcgis.carto.FeatureLayer;
import com.esri.arcgis.carto.esriSelectionResultEnum;
import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.geodatabase.ICursor;
import com.esri.arcgis.geodatabase.IFeature;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFeatureCursor;
import com.esri.arcgis.geodatabase.IFeatureCursorProxy;
import com.esri.arcgis.geodatabase.IFeatureWorkspace;
import com.esri.arcgis.geodatabase.ISelectionSet;
import com.esri.arcgis.geodatabase.QueryFilter;
import com.esri.arcgis.geodatabase.SpatialFilter;
import com.esri.arcgis.geodatabase.esriSpatialRelEnum;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;

/**
 * 查找爱达荷州里有州际公路通过并且change>15的县。<br>
 * 注：这个程序中文译本的例子少了一句pSpatialFilter.SpatialRel = esriSpatialRelCrosses<br>
 * 示例章节：9.5.2
 * @author zhushijie
 *
 */
public class IntersectSelect {

	public static void main(String[] args) throws UnknownHostException, IOException {
		//初始化AE许可
		EngineInitializer.initializeVisualBeans();
		AoInitialize ao = new AoInitialize();
		ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);

		//导入数据
		ShapefileWorkspaceFactory pWF = new ShapefileWorkspaceFactory();
		String ecliseWorkspace = System.getenv("Eclipse3.6Workspace");
		IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace) pWF
				.openFromFile(ecliseWorkspace + "HelloAE" + File.separator
						+ "DataSet" + File.separator + "chap09", 0);

		IFeatureClass pRoadClass = pFeatureWorkspace
				.openFeatureClass("idroads.shp");
		IFeatureClass pCountyClass = pFeatureWorkspace
				.openFeatureClass("idcounty.shp");
		FeatureLayer pRoadLayer = new FeatureLayer();
		pRoadLayer.setFeatureClassByRef(pRoadClass);
		FeatureLayer pCountyLayer = new FeatureLayer();
		pCountyLayer.setFeatureClassByRef(pCountyClass);
		
		//属性查询
		QueryFilter pQueryFilter = new QueryFilter();
		pQueryFilter.setWhereClause("Route_Desc = 'Interstate'");
		pRoadLayer.selectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, false);
		ISelectionSet pRoadSelSet = pRoadLayer.getSelectionSet();
		ICursor[] pCursor = new ICursor[1];
		pRoadSelSet.search(null, false, pCursor);
		
		SpatialFilter pSpatialFilter = new SpatialFilter();
		pSpatialFilter.setWhereClause("change > 15");
		pSpatialFilter.setSpatialRel(esriSpatialRelEnum.esriSpatialRelCrosses);//书中缺少这一句的vba代码
		
		IFeatureCursor pRoadCursor = new IFeatureCursorProxy(pCursor[0]);
		IFeature pRoad = pRoadCursor.nextFeature();
		
		while(pRoad != null){
			pSpatialFilter.setGeometryByRef(pRoad.getShape());
			pCountyLayer.selectFeatures(pSpatialFilter, esriSelectionResultEnum.esriSelectionResultAdd, false);
			pRoad = pRoadCursor.nextFeature();
		}
		System.out.println(pCountyLayer.getSelectionSet().getCount());
		
		//以上代码已完成所有查询功能，下面的代码仅仅用来显示
        MapBean map = new MapBean();
        //添加图层
//        map.addLayer(pRoadLayer, 0);
        map.addLayer(pCountyLayer, 0);
        
        JFrame frame = new JFrame("Hello Arcobjects");
        frame.add(map, BorderLayout.CENTER);
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
	}

}
